/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lm.base.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public final class JPAUtil {

    private static EntityManagerFactory FABRICA;

    @SuppressWarnings("CallToThreadDumpStack")
    protected static void carregarFabrica() {
        if (FABRICA == null) {
            try {
                FABRICA = Persistence.createEntityManagerFactory("lmCondominiosPU");
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("Erro na criação da Fabrica: " + e.getMessage());
            }
        }
    }

    public static EntityManager getSession() {
        if (FABRICA == null) {
            throw new IllegalStateException("EntityManagerFactory, Fabrica ainda não carregada.");
        } else {
            return FABRICA.createEntityManager();
        }
    }

    public synchronized static void destruirFabrica() {
        if (FABRICA != null) {
            FABRICA.close();
        }
    }
    
}
